# -*- coding: utf-8 -*-
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response
from service.services import *
from settings import *

def changeverify(request, shopmaker):
    if not request.user.is_authenticated():
        return HttpResponseRedirect("login/")
    if request.user.is_superuser and request.method == "POST":
        idElem = request.POST["id"]
        value = request.POST["value"]
        nullValue = u'Отсуствует'
        position = idElem.split("_")
        products = getProductTablebyUsername(shopmaker)
        productValue = products[0][int(position[0])][csvHeader[int(position[1])]]
        if value == "null" and unicode(productValue, 'utf-8') == "":
            return HttpResponse(True)
        elif value == "null":
            return HttpResponse(False)
        if isFloat(str(productValue)):
            if float(productValue) == value:
                return HttpResponse(True)
            else:
                return HttpResponse(False)
        if unicode(productValue, 'utf-8') == value or (value == nullValue and productValue == ""):
            return HttpResponse(True)
        else:
            return HttpResponse(False)
    return HttpResponseRedirect("/")


def result(request, shopmaker):
    if not request.user.is_authenticated():
        return HttpResponseRedirect("login/")
    if request.method == "POST":
        filename = request.POST["filename"]
        contentFile = request.POST["content"]
        contentFile = "\"\",%s\r\n%s" % (getCSVHeader(), contentFile)
        if contentFile and filename:
            content = {"filepath": STATIC_URL + "csv/temp/" + shopmaker + "/" + filename, 'shopmaker': shopmaker,
                       'currency': currency_rate}
            if request.user.is_superuser:
                content["admin"] = True
            filenameCsv = saveFile(STATICFILES_DIRS[0] + "/csv/temp/" + shopmaker + "/", filename, contentFile)
            filenameXml = saveFile(STATICFILES_DIRS[0] + "/xml/" + shopmaker + "/", filename.replace("csv", "xml"),
                csvToXml(contentFile), format="xml")
            content["fileXML"] = STATIC_URL + "xml/" + shopmaker + "/" + filenameXml
            return render_to_response("result.html", content)
    return HttpResponseRedirect("/")


def history(request, shopmaker):
    if not request.user.is_authenticated():
        return HttpResponseRedirect("login/")
    csvFilesName = getCSVHistory(shopmaker)
    content = {'shopmaker': shopmaker, 'currency': currency_rate, 'csvDate': csvFilesName}
    if request.user.is_superuser:
        content['admin'] = "true"
    return render_to_response("history.html", content)


def historyView(request, shopmaker):
    if not request.user.is_authenticated():
        return HttpResponseRedirect("login/")
    if request.method == "POST":
        content = historyAction(request.POST["action"].encode("utf-8"), request.POST["filename"], shopmaker)
        if request.user.is_superuser:
            content['admin'] = "true"
        if content['action']:
            return render_to_response('history.html', content)
        return render_to_response("result.html", content)
    return HttpResponseRedirect("/")
